<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<style>
.uploadify-button {
	width: 30px;
}

.uploadify-queue-item {
	background: #fff;
}

.upload-btn {
	background-color: red;
}
</style>
<script type="text/javascript">
    var taskDataGrid;
    var dialog;
    $(function() {
        taskDataGrid = $('#taskDataGrid').datagrid({
            url : '${path}/process/todoTask?userId=<shiro:principal property = "id"/>',
            striped : true,
            rownumbers : true,
            pagination : true,
            singleSelect : true,
            idField : 'id',
            sortName : 'id',
            sortOrder : 'asc',
            pageSize : 20,
            pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
            columns : [ [
            	{
                    field: 'processInstanceId',
                    title: '流程ID',
                    width:100
                }, {
                    field: 'title',
                    title: '标题',
                    width:200
                }, {
                    field: 'businessType',
                    title: '类型',
                    width:100
                }, {
                    field: 'user_name',
                    title: '申请人',
                    width:100
                }, {
                    field: 'taskName',
                    title: '任务名称',
                    width:100
                },{
                    field: 'assigneeOrCandidateUser',
                    title: '任务审批人',
                    width:100,
                    hidden:true
                }, {
                    field: 'createTime',
                    title: '任务创建时间',
                    width:100,
                    formatter: function(value, row, index) {
                    	if(value==null){
                     	   	 return "";
                     	 	}
                        var date=new Date(value);
      	               	 var y = date.getFullYear();
      	               	 var m = date.getMonth() + 1;  
      	               	 if(m < 10) m = "0" + m;
      	               	 var d = date.getDate();  
      	               	 if(d < 10) d = "0" + d;
      	               	 var h = date.getHours();
      	               	 if(h < 10) h = "0" + h;
      	               	 var min = date.getMinutes();
      	               	 if(min < 10) min = "0" + min;
      	               	 var s=date.getSeconds();
      	               	 if(s<10)s="0"+s;
                      return y+"/"+m+"/"+d+" "+h+":"+min+":"+s;
                   }
                }, {
                    field: 'supended',
                    title: '流程状态',
                    width:100,
                    formatter:function(value,row,index){
                    	return value==true?'已挂起':'正常'
                    }
            	}, {
                        field: 'version',
                        title: '版本号'
                },
                {
                    field: 'status',
                    title: '审批状态',
                    width:100,
                    formatter:function(value,row,index){
                    	if(value=='APPROVAL_ADJUST'){
                    		return "待调整"
                    	}
                    	return "待审批"
                    }
            	},
                {
                    field : 'action',
                    title : '操作',
                    width : 220,
                    formatter : function(value, row, index) {
                         var str = '';
                         console.log(row.assign)
                         if(isNull(row.assign)){
                        	 str += $.formatString('<a href="javascript:void(0)" class="usertask-easyui-linkbutton-claim" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'" onclick="claim(\'{0}\')">签收</a>',row.taskId);
                         }else{
                        	  str += $.formatString('<a href="javascript:void(0)" class="usertask-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'"  onclick="handlerTodo(\'{0}\',\'{1}\',\'{2}\',\'{3}\',\'{4}\')">办理</a>',row.businessKey,row.taskId,row.businessType,row.taskName,row.processInstanceId);
                         }
                                str += '&nbsp;&nbsp;&nbsp;&nbsp;'; 
                               str += $.formatString('<a href="${ctxPath}/process/graphHistoryProcessInstance?processInstanceId='+row.processInstanceId+'" target="_blank" class="usertask-easyui-linkbutton-view" data-options="plain:true,iconCls:\'fi-zoom-in icon-green\'"  >查看流程图</a>');
                		return str;
                    }
                }
            ]],
            onLoadSuccess:function(data){
                $('.usertask-easyui-linkbutton-claim').linkbutton({text:'签收'});
                $('.usertask-easyui-linkbutton-edit').linkbutton({text:'办理'});
                $('.usertask-easyui-linkbutton-view').linkbutton({text:'查看流程图'});
            },
            toolbar : '#taskListToolbar'
        });
    });

    function queryHistoryCommentFun(processInstanceId){
        var dialog=$('<div>').dialog({
            title : '查看历史批注',
            width : '50%',
            height : '50%',
            modal:true,
            onClose : function() {
                $(this).dialog('destroy');
            },
            href : '${path}/process/getCommentsByProcessInstanceId?processInstanceId='+processInstanceId,
            buttons : [ {
                text : '关闭',
                handler : function() {
                    dialog.dialog('close');
                }
            } ]
        });
    }
    function handlerTodo(businessKey,taskId,businessType,taskName,processInstanceId){
    	var title=businessType+"-"+taskName;
    	dialog=parent.$.modalDialog({
            title : title,
            width : '80%',
            height : '80%',
            iconCls:'fi-social-joomla',
            modal:true,
            href : '${path}/example/'+businessKey+'/toApproval?taskId='+taskId+'&processInstanceId='+processInstanceId,
            buttons : [ {
                text : '确定',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = taskDataGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#'+businessKey+'handleForm');
                    f.submit();
                }
            } ]
        });
    }
   

    function claim(taskId){
    	$.ajax({
    		url:'${ctxPath}/task/claim/'+taskId,
    		dataType:'json',
    		data:{"userId":'<shiro:principal property = "id"/>'},
    		success:function(data){
    			if(data.success){
    				showMsg(data.msg);
    				taskDataGrid.datagrid('reload');
    			}else{
    				$.messager.alert('错误',data.msg,'error');
    			}
    		}
    	})
    }

    //反签收
    function unclaim() {
        var rows = taskDataGrid.datagrid('getSelections');
        if(rows.length==1){
            $.ajax({
                url:'${ctxPath}/task/unclaim/'+rows[0].taskId,
                dataType:'json',
                success:function(data){
                    if(data.msg){
                        showMsg(data.msg);
                        taskDataGrid.datagrid('reload');
                    }else{
                        $.messager.alert('错误',data.msg,'error');
                    }
                }
            })
        }else{
            $.messager.alert('提示','请选择一条记录','info');
        }
    }
    //转办
    function doTransferTask(){
    	 var rows = taskDataGrid.datagrid('getSelections');
    	 if(rows.length==1){
    		 	var url="${ctxPath}/user/toSelectUser?userids=";
    		    var title='选择转办人';
    		    var width=250;
    		    var dialog= $('<div>').dialog({
    		        title:title,
    		        href:url,
    		        width : width,
    		        height : 500,
    		        modal:true,
    		        onClose : function() {
    		            $(this).dialog('destroy');
    		        },
    		        buttons:[{
                        text:'关闭',
                        handler:function () {
                            dialog.dialog('close');
                        }
                    },{
    		            text:'确定',
    		            handler:function () {
    		                    var treeObj = $.fn.zTree.getZTreeObj("singleUserTree"); //获取全部节点数据
    		                    var nodes = treeObj.getCheckedNodes(true);
    		                    var id = nodes[0].id;
    		                    var name = nodes[0].text;
    		                    id = id.replace('USER', '');//用户id
    		                   	if(!isNull(id)){
    		                   		$.ajax({
    		                   			url:'${ctxPath}/task/transferTask/'+rows[0].taskId+'?userId='+id,
    		                   			dataType:'json',
    		                   			success:function(data){
    		                   				if(data.success){
    		                   					showMsg(data.msg);
    		                   					taskDataGrid.datagrid('reload');
    		                   				}else{
    		                   					$.messager.alert('错误',data.msg,'error');
    		                   				}
    		                   			}
    		                   		})
    		                   	}
    		            		dialog.dialog('close');
    		            		parent.dialog.dialog('close');
    		            }
    		        }]
    		    })
    	 }else{
    		 $.messager.alert('提示','请选择一条记录','info');
    	 }
    }
    //委派
    function doDelegateTask(){
    	 var rows = taskDataGrid.datagrid('getSelections');
		 if(rows.length==1){
			 var url="${ctxPath}/user/toSelectUser?userids=";
 		    var title='选择委派人';
 		    var width=250;
 		    var dialog= $('<div>').dialog({
 		        title:title,
 		        href:url,
 		        width : width,
 		        height : 500,
                modal:true,
 		        onClose : function() {
 		            $(this).dialog('destroy');
 		        },
 		        buttons:[
                    {
                      text:'关闭',
                      handler:function () {
                          dialog.dialog('close');
                      }
                    },
 		            {
 		            text:'确定',
 		            handler:function () {
 		                    var treeObj = $.fn.zTree.getZTreeObj("singleUserTree"); //获取全部节点数据
 		                    var nodes = treeObj.getCheckedNodes(true);
 		                    var id = nodes[0].id;
 		                    var name = nodes[0].text;
 		                    id = id.replace('USER', '');//用户id
 		                   	if(!isNull(id)){
 		                   		$.ajax({
 		                   			url:'${ctxPath}/task/delegateTask/'+rows[0].taskId+'?userId='+id,
 		                   			dataType:'json',
 		                   			success:function(data){
 		                   				if(data.success){
 		                   					showMsg(data.msg);
 		                   					taskDataGrid.datagrid('reload');
 		                   				}else{
 		                   					$.messager.alert('错误',data.msg,'error');
 		                   				}
 		                   			}
 		                   		})
 		                   	}
 		            		dialog.dialog('close');
 		            		parent.dialog.dialog('close');
 		            }
 		        }]
 		    })
    	 }else{
    		 $.messager.alert('提示','请选择一条记录','info');
    	 }
    }

    function fallbackFun(processInstanceId,procDefId,taskId,taskDefKey){
        var dialog=$('<div>').dialog({
            title : '回退',
            width : '50%',
            height : '50%',
            modal:true,
            onClose : function() {
                $(this).dialog('destroy');
            },
            href : '${path}/task/initJumpTaskNodes?flag=false&procDefId='+procDefId+'&taskDefKey='+taskDefKey,
            buttons : [ {
                text : '关闭',
                handler : function() {
                    dialog.dialog('close');
                }
            } ,{
                text : '确定',
                handler : function() {
                    var targetTaskDefinitionKey=$('#taskJump [name=targetTaskDefinitionKey]:checked').val();
                    var comment=$('#taskJump [name=comment]').val();
                    if(isNull(comment)){
                        $.messager.alert('提示',"跳转内容不能为空！",'info');
                        return;
                    }
                    if(isNull(targetTaskDefinitionKey)) {
                        $.messager.alert('提示',"请选择要跳转的节点！",'info');
                        return;
                    }
                        $.ajax({
                            url:'${ctxPath}/task/jumpTask',
                            data:{"targetTaskDefinitionKey":targetTaskDefinitionKey,
                                "currentTaskId":taskId,"comment":comment,"flag":false},
                            dataType:'json',
                            success:function(data){
                                if(data.success){
                                    showMsg(data.msg);
                                    taskDataGrid.datagrid('load',{});
                                }else{
                                    $.messager.alert('错误',data.msg,'error');
                                }
                            }
                        })
                        dialog.dialog('close');
                        parent.dialog.dialog('close');
                }
            } ]
        });
    }
    function forwardFun(processInstanceId,procDefId,taskId,taskDefKey){
        var dialog=$('<div>').dialog({
            title : '前进',
            width : '40%',
            height : '40%',
            modal:true,
            onClose : function() {
                $(this).dialog('destroy');
            },
            href : '${path}/task/initJumpTaskNodes?flag=true&procDefId='+procDefId+'&taskDefKey='+taskDefKey,
            buttons : [ {
                text : '关闭',
                handler : function() {
                    dialog.dialog('close');
                }
            } ,{
                text : '确定',
                handler : function() {
                    var targetTaskDefinitionKey=$('#taskJump [name=targetTaskDefinitionKey]:checked').val();
                    var comment=$('#taskJump [name=comment]').val();
                    if(isNull(comment)){
                        $.messager.alert('提示',"备注内容不能为空！",'info');
                        return;
                    }
                    if(isNull(targetTaskDefinitionKey)) {
                        $.messager.alert('提示',"请选择要跳转的节点！",'info');
                        return;
                    }
                    $.ajax({
                        url:'${ctxPath}/task/jumpTask',
                        data:{"targetTaskDefinitionKey":targetTaskDefinitionKey,
                            "currentTaskId":taskId,"comment":comment,"flag":true},
                        dataType:'json',
                        success:function(data){
                            if(data.success){
                                showMsg(data.msg);
                                taskDataGrid.datagrid('load',{});
                            }else{
                                $.messager.alert('错误',data.msg,'error');
                            }
                        }
                    })
                    dialog.dialog('close');
                    parent.dialog.dialog('close');
                }
            } ]
        });
    }
</script>
<div class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:true">
        <table id="taskDataGrid" data-options="fit:true,border:false"></table>
    </div>
</div>
<div id="taskListToolbar" style="display: none;">
    <a onclick="doTransferTask();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-sound icon-blue'">转办</a>
    <a onclick="doDelegateTask();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-comments icon-blue'">委派</a>
    <a onclick="unclaim();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-bluetooth icon-blue'">反签收</a>
</div>
